<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class sfTableBaseFormFilter extends sfFormFilterDoctrine
{
    
    public $filterTypes = array(); 
    public $filterFields = array();     
    public $model; 
    public $parent;
    
    public function setup()
    {
    }

    public function getFields()
    {
      return $this->filterTypes;
    }

    public function getModelName()
    {
      return $this->model;
    }
    
  /**
   * Sets the parent table
   *
   * @param  sfTable|null $sfTable
   *
   * @return sfTable The current widget instance
   */
  public function setParent(sfTable $sfTable = null)
  {
    $this->parent = $sfTable;

    return $this;
  }

  /**
   * Returns the parent table.
   *
   * If no table has been set with setParent(), NULL is returned.
   *
   * @return sfTable|null
   */
  public function getParent()
  {
    return $this->parent;
  }
    
    
    
  /**
   * Esta rutina sobreescribe la original del sfFormFilterDoctrine
   *
   *
   * @param  array $values
   *
   * @return Doctrine_Query
   */
    
  protected function doBuildQuery(array $values)
  {
    $query = isset($this->options['query']) ? clone $this->options['query'] : $this->getTable()->createQuery('r');

    $fields = $this->getFields();


    foreach ($fields as $field => $type)
    {
      if (!isset($values[$field]) || null === $values[$field] || '' === $values[$field])
      {
        
      }else
      {

        if (null !== $type)
        {
          if (!method_exists($this, $method = sprintf('add%sQuery', $type)))
          {
            throw new LogicException(sprintf('Unable to filter for the "%s" type.', $type));
          }

          $this->$method($query, $field, $values[$field]);
        }
      }
    }

    return $query;
  }
  
  
  
  

  protected function addForeignKeyQuery(Doctrine_Query $query, $field, $value)
  {
    $fieldName = $this->getFieldName($field);

    if (is_array($value))
    {
      $query->andWhereIn(sprintf('%s.%s', $query->getRootAlias(), $fieldName), $value);
        $this->parent->thSchema[$field]->setFilter();      
    }
    else
    {
      $query->addWhere(sprintf('%s.%s = ?', $query->getRootAlias(), $fieldName), $value);
        $this->parent->thSchema[$field]->setFilter();      
    }
  }

  protected function addEnumQuery(Doctrine_Query $query, $field, $value)
  {
    $fieldName = $this->getFieldName($field);

    $query->addWhere(sprintf('%s = ?', $this->filterFields[$field])  , $value);
    $this->parent->thSchema[$field]->setFilter();    
  }
  
  
  
  public function addMultiQuery (Doctrine_Query $query, $field, $values){
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query->andWhereIn($this->filterFields[$field], $values);
    $this->parent->thSchema[$field]->setFilter();            
  }
  

  protected function addTextQuery(Doctrine_Query $query, $field, $values)
  {
    //$fieldName = $this->getFieldName($field);

    if (is_array($values) && isset($values['is_empty']) && $values['is_empty'])
    {
      $query->addWhere(sprintf('(%s IS NULL OR %1$s = ?)', $this->filterFields[$field]), array(''));
        $this->parent->thSchema[$field]->setFilter();      
    }
    else if (is_array($values) && isset($values['text']) && '' != $values['text'])
    {
      $query->addWhere(sprintf('%s LIKE ?', $this->filterFields[$field]), '%'.addcslashes($values['text'], '\'\%_').'%');
        $this->parent->thSchema[$field]->setFilter();      
    }
  }

  
  
  protected function addMultiTextQuery(Doctrine_Query $query, $field, $values)
  {
    //$fieldName = $this->getFieldName($field);

    if (is_array($values) && isset($values['is_empty']) && $values['is_empty'])
    {
      $query->addWhere(sprintf('(%s IS NULL OR %1$s = ?)', $this->filterFields[$field]), array(''));
        $this->parent->thSchema[$field]->setFilter();      
    }
    else if (is_array($values) && isset($values['text']) && '' != $values['text'])
    {
        
        
        $arrayOfWords = explode(" ",$values['text']);

        foreach($arrayOfWords as $word)
        {
            $query->andWhere(sprintf('%s LIKE ?', $this->filterFields[$field]), '%'.addcslashes($word, '\'\%_').'%');
            $this->parent->thSchema[$field]->setFilter();            
        }
    }
  }
  
  protected function addNumberQuery(Doctrine_Query $query, $field, $values)
  {
    $fieldName = $this->getFieldName($field);

    if (is_array($values) && isset($values['is_empty']) && $values['is_empty'])
    {
      $query->addWhere(sprintf('(%s IS NULL OR %1$s = ?)', $this->filterFields[$field]), array(''));
        $this->parent->thSchema[$field]->setFilter();      
    }
    else if (is_array($values) && isset($values['text']) && '' !== $values['text'])
    {
      $query->addWhere(sprintf('%s = ?', $this->filterFields[$field]), $values['text']);
        $this->parent->thSchema[$field]->setFilter();      
    }
  }

  protected function addBooleanQuery(Doctrine_Query $query, $field, $value)
  {
    $fieldName = $this->getFieldName($field);
    $query->addWhere(sprintf('%s = ?', $this->filterFields[$field]), $value);
    $this->parent->thSchema[$field]->setFilter();          
  }

  protected function addDateRangeQuery(Doctrine_Query $query, $field, $values)
  {
      
    $fieldName = $this->getFieldName($field);

    if (isset($values['is_empty']) && $values['is_empty'])
    {
      $query->addWhere(sprintf('%s IS NULL', $this->filterFields[$field]));
    }
    else
    {
      if (null !== $values['from'] && null !== $values['to'])
      {
        $query->andWhere(sprintf('%s >= ?', $this->filterFields[$field]), $values['from']);
        $query->andWhere(sprintf('%s <= ?', $this->filterFields[$field]), $values['to']);
        $this->parent->thSchema[$field]->setFilter();
      }
      else if (null !== $values['from'])
      {
        $query->andWhere(sprintf('%s >= ?', $this->filterFields[$field]), $values['from']);
        $this->parent->thSchema[$field]->setFilter();        
      }
      else if (null !== $values['to'])
      {
        $query->andWhere(sprintf('%s <= ?', $this->filterFields[$field]), $values['to']);
        $this->parent->thSchema[$field]->setFilter();        
      }
    }
  }
 
  //abstract public function addDatosAfiliadoColumnQuery (Doctrine_Query $query, $field, $values);  
}




?>
